package com.example.administrator.fishbone.Activity;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.fishbone.Fragment.IFragment;
import com.example.administrator.fishbone.Fragment.AppFragment;
import com.example.administrator.fishbone.Fragment.ContactFragment;
import com.example.administrator.fishbone.Fragment.homeFragment;
import com.example.administrator.fishbone.R;
import com.example.administrator.fishbone.bean.BackHandledFragment;
import com.example.administrator.fishbone.bean.BackHandledInterface;

public class MainActivity extends FragmentActivity implements BackHandledInterface {
RadioGroup radioGroup;
    RadioButton radioButton;
    homeFragment home;
    AppFragment app;
    ContactFragment contact;
    IFragment i;
    FragmentManager fm = null;

    private static MainActivity instance;
    private BackHandledFragment backHandledFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
fm = getSupportFragmentManager();
        initView();

    }
private void initView(){
    radioGroup = (RadioGroup) findViewById(R.id.radio_group);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            FragmentTransaction ft = fm.beginTransaction();
            hide(ft);
            switch (checkedId) {
                case R.id.radio_btn1:
                    if (home == null) {
                        home = new homeFragment();
                        ft.add(R.id.fl_main, home);
                    }
                    else {
                        ft.show(home);
                    }
                    break;
                case R.id.radio_btn2:
                    if (contact == null) {
                        contact = new ContactFragment();
                        ft.add(R.id.fl_main, contact);
                    }

                    else {
                        ft.show(contact);
                    }
                    break;
                case R.id.radio_btn3:
                    if (app == null) {
                        app = new AppFragment();
                        ft.add(R.id.fl_main, app);
                    }
                    else {
                        ft.show(app);
                    }
                    break;
                case R.id.radio_btn4:
                    if (i == null) {
                        i = new IFragment();
                        ft.add(R.id.fl_main, i);
                    }
                    else {
                        ft.show(i);
                    }
                    break;
            }
        ft.commit();
        }

    });
    radioButton = (RadioButton) findViewById(R.id.radio_btn1);
    radioButton.setChecked(true);
}

    private void hide(FragmentTransaction ft) {
        if (home != null) {
            ft.hide(home);
        }
        if (contact != null) {
            ft.hide(contact);
        }
        if (app != null) {
            ft.hide(app);
        }
        if (i != null) {
            ft.hide(i);
        }
    }


    public static MainActivity getInstance() {
        if (instance == null) {
            instance = new MainActivity();
        }
        return instance;
    }

    @Override
    public void setSelectedFragment(BackHandledFragment selectedFragment) {
this.backHandledFragment = selectedFragment;
    }

    @Override
    public void onBackPressed() {
        if (backHandledFragment == null || !backHandledFragment.onBackPressed()) {
            if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                super.onBackPressed();

            } else {
                getSupportFragmentManager().popBackStack();
            }

        }
    }
}
